/**
 * 
 */
package com.xb.cloud.product.merchant.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xb.cloud.common.client.bo.UpdateInfo;
import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.util.BeanUtils;
import com.xb.cloud.order.service.client.bo.SoSkuSummeryBo;
import com.xb.cloud.product.merchant.inner.service.ProductSkuInnerService;
import com.xb.cloud.product.merchant.service.client.ProductSkuService;
import com.xb.cloud.product.merchant.service.client.bo.EndMerchantUserBo;
import com.xb.cloud.product.merchant.service.client.bo.ProductSkuBo;
import com.xb.cloud.product.merchant.service.client.bo.ShopSkuBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuInputBo;
import com.xb.cloud.product.service.business.model.ProductSku;
import com.xb.cloud.product.service.business.model.ProductSkuExample;
import com.xb.cloud.product.service.business.model.mapper.ProductSkuMapper;
import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;

/**
 * @author kexue
 *
 */
@Service
public class ProductSkuServiceImpl extends AbstractService  implements ProductSkuService{

	@Resource
	private ProductSkuInnerService  productSkuInnerService;
	
	@Resource
	ProductSkuMapper productSkuMapper;
	/**
	 * 保存sku 
	 */
	@Override
	public ServiceReturnInfo createSku(ProductSkuInputBo skuInput,EndMerchantUserBo endMerchantUserBo) {
		
		Long id = productSkuInnerService.createSku(skuInput, endMerchantUserBo);
		if(id>0){
			return ServiceReturnUtils.success(id);
		}else{
			return ServiceReturnUtils.error(ServiceReturnCodes.SYSTEM_EXCEPTION);
		}
	} 
	
	/*
	 * 根据id查询ProductSku
	 */
	@Override
	public ServiceReturnInfo getSkuById(Long skuId) {
		ProductSkuExample productSkuExample = new ProductSkuExample();
		ProductSkuExample.Criteria criteria=productSkuExample.createCriteria();
		criteria.andIsDeletedEqualTo(DbConstants.IS_DELETED_FALSE);
		criteria.andIdEqualTo(skuId);
		List<ProductSku> productSkuList=productSkuMapper.selectByExample(productSkuExample);
		if(productSkuList==null||productSkuList.size()<0){
			return ServiceReturnUtils.error(ServiceReturnCodes.PRODUCT_MANAGE_SKU_NOT_EXIST);
		} else {
			return ServiceReturnUtils.success(BeanUtils.newAndCopyProperties(ProductSkuBo.class,productSkuList.get(0)));
		}
	}

	@Override
	public ServiceReturnInfo getSkuListByIds(List<Long> skuIds) {
		if(skuIds==null)
			return ServiceReturnUtils.error(ServiceReturnCodes.PARAM_IS_NULL);
		if(skuIds.size()==0)
			return ServiceReturnUtils.success(new ArrayList<ShopSkuBo>());
		List<ShopSkuBo> skuList=productSkuMapper.getShopSkuListBySkuIds(skuIds);
		if(skuList==null)
			return ServiceReturnUtils.error(ServiceReturnCodes.SYSTEM_EXCEPTION);
		return ServiceReturnUtils.success(skuList);
	}

	
	/**
	 * 更新销量数据
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED,value="transactionManager_product")
	public ServiceReturnInfo updateTotalSaleAmout(List<SoSkuSummeryBo> sellOrderSkuBoList,
			EndMerchantUserBo endMerchantUserBo) {
		 
		UpdateInfo updateInfo=new UpdateInfo();
		updateInfo.setUpdateTime(super.getTimeService().getDate());
		updateInfo.setUpdateUserId(endMerchantUserBo.getId());
		updateInfo.setUpdateUserName(endMerchantUserBo.getLoginName()); 

		int updateRow = productSkuMapper.batchUpdateTotalSaleAmout(sellOrderSkuBoList, updateInfo);
		if(updateRow<=0){
			return ServiceReturnUtils.error(ServiceReturnCodes.SYSTEM_EXCEPTION);
		} else {
			return ServiceReturnUtils.success(1);
		}
	}
}
